package com.interview.checkinfo.repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import com.interview.checkinfo.entity.CheckInfo;
import com.interview.checkinfo.entity.CheckInfo_;
import com.interview.checkinfo.query.CheckInfoConditionQuery;
import com.interview.checkinfo.query.Page;
import com.interview.employeeInfo.entity.EmployeeInfo;

@Repository
public class CheckInfoRepository {

	@PersistenceContext
	private EntityManager entityManager;

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	public Page dynamicQuery(CheckInfoConditionQuery checkInfoConditionQuery) {

		String employeeName = checkInfoConditionQuery.getEmployeeName();

		Date checkInTimeEnd = checkInfoConditionQuery.getCheckInTimeEnd();
		Date checkInTimeStar = checkInfoConditionQuery.getCheckInTimeStar();

		Date checkOutTimeEnd = checkInfoConditionQuery.getCheckOutTimeEnd();
		Date checkOutTimeStar = checkInfoConditionQuery.getCheckOutTimeStar();

		Date createdEnd = checkInfoConditionQuery.getCreatedEnd();
		Date createStar = checkInfoConditionQuery.getCreatedStar();

		Integer pageIndex = checkInfoConditionQuery.getPageIndex();

		List<Predicate> queryCon = new ArrayList<Predicate>();

		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

		CriteriaQuery<CheckInfo> query = criteriaBuilder
				.createQuery(CheckInfo.class);

		Root<CheckInfo> root = query.from(CheckInfo.class);

		if (employeeName != null) {

			Predicate predicateName = criteriaBuilder
					.like(root.get(CheckInfo_.employeeName), "%" + employeeName
							+ "%");
			queryCon.add(predicateName);
		}

		if (createStar != null) {

			Predicate createdTimeStar = criteriaBuilder.greaterThanOrEqualTo(
					root.get(CheckInfo_.createdTime), createStar);

			queryCon.add(createdTimeStar);
		}

		if (createdEnd != null) {

			Predicate createdTimeEnd = criteriaBuilder.lessThanOrEqualTo(
					root.get(CheckInfo_.createdTime), createdEnd);

			queryCon.add(createdTimeEnd);

		}

		if (checkInTimeStar != null) {
			Predicate checkInStar = criteriaBuilder.greaterThanOrEqualTo(
					root.get(CheckInfo_.chcekInTime), checkInTimeStar);

			queryCon.add(checkInStar);
		}

		if (checkInTimeEnd != null) {
			Predicate checkInEnd = criteriaBuilder.lessThanOrEqualTo(
					root.get(CheckInfo_.chcekInTime), checkInTimeEnd);

			queryCon.add(checkInEnd);
		}

		if (checkOutTimeEnd != null) {
			Predicate checkoutEnd = criteriaBuilder.lessThanOrEqualTo(
					root.get(CheckInfo_.chcekOutTime), checkOutTimeEnd);

			queryCon.add(checkoutEnd);
		}

		if (checkOutTimeStar != null) {
			Predicate checkOutStar = criteriaBuilder.greaterThanOrEqualTo(
					root.get(CheckInfo_.chcekOutTime), checkOutTimeStar);
			queryCon.add(checkOutStar);
		}

		query.where(criteriaBuilder.and(queryCon.toArray(new Predicate[queryCon
				.size()])));

		TypedQuery<CheckInfo> type = entityManager.createQuery(query);

		Integer total = type.getResultList().size();

		if (pageIndex != 0) {

			type.setFirstResult(pageIndex * 10).setMaxResults(10);

		} else {

			type.setFirstResult(0).setMaxResults(10);

		}

		List<CheckInfo> checkInfoes = type.getResultList();

		Page page = new Page();
		page.setTotal(total);
		page.setPageIndex(pageIndex);
		page.setList(checkInfoes);

		return page;
	}

	public void save(CheckInfo checkInfo) {
		entityManager.persist(checkInfo);

	}

	public void delet(String id) {

		entityManager.remove(findOne(id));

	}

	public CheckInfo update(CheckInfo checkInfo) {
		return entityManager.merge(checkInfo);

	}

	public CheckInfo findOne(String id) {
		return entityManager.find(CheckInfo.class, id);
	}

	public List<CheckInfo> dynamicQueryfindByEnployeeInfo(
			EmployeeInfo employeeInfo) {


		List<Predicate> queryCon = new ArrayList<Predicate>();

		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

		CriteriaQuery<CheckInfo> query = criteriaBuilder
				.createQuery(CheckInfo.class);

		Root<CheckInfo> root = query.from(CheckInfo.class);

		if (employeeInfo != null) {

			Predicate predicateName = criteriaBuilder
					.equal(root.get(CheckInfo_.employeeInfo),   employeeInfo
							 );
			queryCon.add(predicateName);
		}

		query.where(criteriaBuilder.and(queryCon.toArray(new Predicate[queryCon
				.size()])));

		TypedQuery<CheckInfo> type = entityManager.createQuery(query);

		
	
		List<CheckInfo> checkInfoes = type.getResultList();

		return checkInfoes;
	}

}
